﻿using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace XnbConverter.XNAGraphicsDeviceControl
{
    public class ModelViewGraphics:GraphicsDeviceControl
    {
        public Matrix Projection;
        public Matrix Viewing;

        public Vector3 CameraPosition = new Vector3(0f, 10f, -50f);
        public Vector3 CameraReferencePosition = new Vector3(0f, 0f, 0f);
        public float CameraAngleYaw = 0f;
        public float CameraAnglePitch = 1f;
        public float CameraDitance = 5f;
        public Microsoft.Xna.Framework.Graphics.Viewport ViewPort;

        VertexDeclaration vertexDeclaration;
        BasicEffect effect;
        Stopwatch timer;
        List<VertexPositionColor[]> aabbGuideList = new List<VertexPositionColor[]>();
        List<Model> modelList=new List<Model>();
        ContentManager contentManager;

        protected override void Initialize()
        {
            // Create our vertex declaration.
            vertexDeclaration = new VertexDeclaration(GraphicsDevice,
                                                VertexPositionColor.VertexElements);
            // Create our effect.
            effect = new BasicEffect(GraphicsDevice, null);

            effect.VertexColorEnabled = true;

            // Start the animation timer.
            timer = Stopwatch.StartNew();

            // Hook the idle event to constantly redraw our animation.
            //Application.Idle += delegate { Invalidate(); };
            //UpdatePanelDrawingInfo();
            UpdateCamera();
        }

        public void UpdateDrawingInfo()
        {
            contentManager = new ContentManager(this.Services, Document.CommonContentBuilder.OutputDirectory);
            contentManager.Unload();
            modelList.Clear();
            foreach(string assetname in Document.AssetFilenames){
                modelList.Add(contentManager.Load<Model>(assetname));
            }            
        }
        public void UpdateCamera()
        {
            Vector3 zv = new Vector3(0f, 0f, -CameraDitance);
            Matrix mh = Matrix.CreateFromYawPitchRoll(CameraAngleYaw, CameraAnglePitch, 0f);
            CameraPosition = Vector3.Transform(zv, mh);
            CameraPosition += CameraReferencePosition;
            Viewing =  Matrix.CreateLookAt(CameraPosition, CameraReferencePosition, Vector3.Up);
            Projection = Matrix.CreatePerspectiveFieldOfView(1.0f, GraphicsDevice.Viewport.AspectRatio, 0.01f, 255f);
        }
        protected override void Draw()
        {
            GraphicsDevice.Clear(Color.DarkGray);

            
            // Set renderstates.
            GraphicsDevice.RenderState.CullMode = CullMode.None;

            GraphicsDevice.VertexDeclaration = vertexDeclaration;

            foreach(Model current_model in modelList)
            {
                foreach (ModelMesh mesh in current_model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.World = Matrix.Identity;
                        effect.View = Viewing;
                        effect.Projection = Projection;
                        effect.EnableDefaultLighting();
                        effect.PreferPerPixelLighting = true;
                        effect.SpecularPower = 16;
                    }
                    mesh.Draw();
                }
            }

        }
    }
}
